package supermarket.resources;

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import supermarket.dao.Shop;
import supermarket.model.Customer;
import supermarket.model.CustomerList;

@Path("/customers")
public class CustomerResource {
	
	private Shop shopService;
	
	public void setShopService(Shop shopService) {
		this.shopService = shopService;
	}
	
	// The Java method will process HTTP POST requests
    @GET
    @Produces({"application/xml", "application/json"})
    public CustomerList getCustomers(@QueryParam("keyword") String keyword) {
    	CustomerList customerList = new CustomerList();
    	List catalog = (List)shopService.getCustomerByName(keyword);
    	customerList.setCatalog(catalog);
    	return customerList;
    }
    
    // The Java method will process HTTP GET requests
    @GET @Path("/list")
    @Produces(MediaType.APPLICATION_XML)
    public CustomerList getCustomerList() {
    	CustomerList customerList = new CustomerList();
    	List catalog = (List)shopService.getCustomerList();
    	customerList.setCatalog(catalog);
    	return customerList;
    }
    
    // The Java method will process HTTP GET requests
    @GET @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Customer getCustomerList(@PathParam("id") Integer id) {
    	return (Customer)shopService.getCustomer(id);
    }
    
    @PUT @Path("/add/{name}")
    public void addCustomer(@PathParam("name") String name) {
    	shopService.addCustomerByName(name);
    }
    
    @DELETE @Path("/remove/{id}")
    public void removeCustomer(@PathParam("id") Integer id) {
    	shopService.removeCustomer(shopService.getCustomer(id));
    }

}
